﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Log;
using TLL.Debug;
using TLL.Finance.Currencys;
using TLL.Finance;

using TasksLog.Conf;

namespace TasksLog
{
    public class Session : IDisposable
    {
        #region Varible

            private List<LogItem> itemsToChange = new List<LogItem>();
            private Timer timer = null;
            private Timer timerLogTools = null;
            private LogItem item = null;
            private Error error = null;
            private bool inProcess = false;

            public LogTime WorkTime = LogTime.Empty,
                           ToDayWorkTime = LogTime.Empty,
                           TaskTime = LogTime.Empty,
                           ToDayTaskTime = LogTime.Empty;

            public int? SelectIndex = null;

        #endregion

        #region Properties

            public LogItem SelectItem { get { return this.item; } set { this.item = value; } }
            public bool HasItem { get { return this.item != null; } }
            public bool InProcess { get { return inProcess; } }
            public Error Error { get { return this.error; } }

        #endregion

        public Session()
        {
            this.timer = new Timer();
            this.timer.Interval = 1000;
            this.timer.Tick += GOClock;
        }
        public bool SaveSession()
        {
            try
            {
                using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
                    if (repository.Access && this.itemsToChange.Count > 0)
                        foreach (var newitem in this.itemsToChange)
                            repository.Update(MainConf.Items.FirstOrDefault(x => x.ID == newitem.ID), newitem);
                    else
                        MessageBox.Show(repository.Error.Message, repository.Error.Title, MessageBoxButtons.OK, repository.Error.MessageBoxIcon);
                MainConf.Session.itemsToChange.Clear();
            }
            catch { return false; }
            return true;
        }
        public void Start()
        {
            try
            {
                if (this.itemsToChange.Contains(SelectItem))
                {

                    this.TaskTime = this.SelectItem.GetTime();
                    this.timer.Start();
                    this.inProcess = true;
                }
                else
                {
                    LogTime toDayTime = LogTime.Empty;
                    try { toDayTime = this.SelectItem.Log.FirstOrDefault(x => x.Date.Value.ToShortDateString() == DateTime.Now.ToShortDateString()).Time; }
                    catch { toDayTime = LogTime.Empty; }
                    this.itemsToChange.Add(this.SelectItem);
                    this.ToDayTaskTime = toDayTime;
                    this.TaskTime = this.SelectItem.GetTime();
                    this.timer.Start();
                    this.inProcess = true;
                }
            }
            catch
            {
                this.error = new Error(
                    ErrorType.SessionStart,
                    new Dictionary<string, string>() 
                        {
                            { "ID", item.ID.ToString() },
                            { "Time", LogTime.Formated(item.GetTime()) }
                        });
                MessageBox.Show(this.error.Message, this.error.Title, MessageBoxButtons.OK, this.error.MessageBoxIcon);
            }
        }
        public void Pause()
        {
            this.timer.Stop();
        }
        public void Stop()
        {
            if (this.item.HasLog && (this.item.Log.FirstOrDefault(x => x.Date.Value.ToShortDateString() == DateTime.Now.ToShortDateString()) != null))
                this.item.Log.FirstOrDefault(x => x.Date.Value.ToShortDateString() == DateTime.Now.ToShortDateString()).Time = this.ToDayTaskTime;
            else
                this.item.AddLog(new LogDate(
                    DateTime.Now, 
                    this.ToDayTaskTime));
            this.timer.Stop();
            this.inProcess = false;
        }

        private void GOClock(object sender, EventArgs e)
        {
            this.TaskTime.Add(1);
            this.ToDayWorkTime.Add(1);
            this.ToDayTaskTime.Add(1);
        }
        private int GetLastIDLogs()
        {
            var res = 1;
            foreach (var item in this.item.Log)
                if (res < item.ID)
                    res = item.ID;

            foreach (var item in this.item.LogChanges)
                if (res < item.ID)
                    res = item.ID;

            return res;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~Session() 
        {
            Dispose(false);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.itemsToChange = null;
                this.WorkTime = null;
                this.ToDayWorkTime = null;
                this.TaskTime = null;
                this.ToDayTaskTime = null;
                this.error = null;
                this.inProcess = false;
                this.SelectIndex = null;
                this.item = null;
                this.timer = null;
            }
        }
    }
}
